package android.gpsmessenger;

import java.io.*;

import android.util.Xml;
import android.app.Activity;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class MessageManager 
{
	private String _xmlFile;
	private Activity _activity;
	
	public MessageManager(Activity activity)
	{
		_activity = activity;
		_xmlFile = "/messages.xml";
	}
	
	public MessageList load() 
	{
		Message message = new Message();
		MessageList messages = new MessageList();
		String id, text;
		XmlPullParser parser = Xml.newPullParser();
		try 
		{
			File myFile = new File(_activity.getFilesDir().getAbsolutePath() + _xmlFile);
			InputStream in = new FileInputStream(myFile);
			
			parser.setInput(in, null);
			
			while (parser.getEventType() != XmlPullParser.END_DOCUMENT)
			{
				if(parser.getEventType() == XmlPullParser.START_TAG && 
		                        parser.getName().equalsIgnoreCase("message"))
				{
					id = parser.getAttributeValue(null, "id");
					text = parser.getAttributeValue(null, "text");
	
					message.setMessage(text);
					
					if (id.equals("sos"))
						messages.setSOSMessage(message);
					else if(id.equals("help"))
						messages.setHelpMessage(message);
					else if(id.equals("message1"))
						messages.setMessage1(message);
					else if(id.equals("message2"))
						messages.setMessage2(message);
				}
				parser.next();
			}
		}
		catch (IOException e) {e.printStackTrace();} 
		catch (XmlPullParserException e) {e.printStackTrace();}   
		
		return messages;
	}	
		
	public void save(MessageList messages)
	{
		try
		{ 	
			String str = writeXML(messages);
			File myFile = new File(_activity.getFilesDir().getAbsolutePath() + _xmlFile);
 
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =  new OutputStreamWriter(fOut);
            myOutWriter.append(str);
            myOutWriter.close();
            fOut.close();
		}
		catch(IOException ioe) {ioe.printStackTrace();}		
	}
	
 	private String writeXML(MessageList messages){
 	    XmlSerializer serializer = Xml.newSerializer();
 	    StringWriter writer = new StringWriter();
 	    try {
 	        serializer.setOutput(writer);
 	        serializer.startDocument("UTF-8", true);
 	        serializer.startTag("", "messages");
 	        
 	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "sos");
 	        serializer.attribute("", "text", messages.getSOSMessage().getMessage());
 	        serializer.endTag("", "message");
 	        
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "help");
 	        serializer.attribute("", "text", messages.getHelpMessage().getMessage());
 	        serializer.endTag("", "message");
 	        
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "message1");
 	        serializer.attribute("", "text", messages.getMessage1().getMessage());
 	        serializer.endTag("", "message");
 	         	       
	        serializer.startTag("", "message");
 	        serializer.attribute("", "id", "message2");
 	        serializer.attribute("", "text", messages.getMessage2().getMessage());
 	        serializer.endTag("", "message");
 	        
 	        serializer.endTag("", "messages");
 	        serializer.endDocument();
 	        return writer.toString();
 	    } 
 	    catch (Exception e) {throw new RuntimeException(e);} 
 	}	
	

}
